﻿using CDH_Shop.Models.Car;
using CDH_Shop.Models.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace CDH_Shop.Services
{
    public class CarService : ICarService
    {
        private readonly CDH_ShopContext _db;
        /// <summary>
        /// 服务类构造函数
        /// </summary>
        /// <param name="db"></param>
        public CarService(CDH_ShopContext db)
        {
            _db = db;
        }
        /// <summary>
        /// 加入购物车方法
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        public bool AddCar(int goodId, int userId)
        {
            //实现加入购物车接口
            var good = _db.Goods.FirstOrDefault(x => x.GoodId == goodId);
            if (good == null)
                return false;
            //新增购物车商品记录
            var car = new Car
            {
                Uid = userId,
                GoodId = goodId,
                Count = 1,
                CreateTime = DateTime.Now,
                GoodCover = good.GoodCover,
                GoodName = good.GoodName,
                Price = good.Price
            };
            _db.Cars.Add(car);
            var row = _db.SaveChanges();
            if (row > 0)
                return true;
            return false;
        }
        /// <summary>
        /// 更新购物车数量
        /// </summary>
        /// <returns></returns>
        public bool UpdateCar(Car car, int count)
        {
            //                      int +/-
            car.Count = car.Count + count;
            _db.Cars.Update(car);
            var row = _db.SaveChanges();
            if (row > 0)
                return true;
            return false;
        }
        /// <summary>
        /// 删除购物车
        /// </summary>
        /// <param name="car"></param>
        /// <returns></returns>
        public bool DeleteCar(Car car)
        {
            _db.Cars.Remove(car);
            var row = _db.SaveChanges();
            return row > 0 ? true : false;
        }
        /// <summary>
        /// 根据用户Id和商品Id获取购物车对象
        /// </summary>
        /// <returns></returns>
        public Car GetCarByUId(int goodId, int uId)
        {
            return _db.Cars.FirstOrDefault(x => x.GoodId == goodId && x.Uid == uId);
        }
        /// <summary>
        /// 获取用的购物车商品列表
        /// </summary>
        /// <returns></returns>
        public List<CarGood> GetCarList(int userId)
        {
            var cars = _db.Cars.Where(x => x.Uid == userId)
                .Select(x => new CarGood
                {
                    GoodId = x.GoodId,
                    GoodCover = x.GoodCover,
                    GoodName = x.GoodName,
                    Price = x.Price,
                    Count = x.Count
                }).ToList();
            return cars;
        }
        /// <summary>
        /// 清空购物车 void空不需要返回值
        /// </summary>
        public void ClearCar(int userId)
        {
            var cars = _db.Cars.Where(x => x.Uid == userId).ToList();
            _db.Cars.RemoveRange(cars);
        }
    }
}
